# 创建库
add_library(demo_lib
    DemoDAO.cpp
)

# 设置目标属性
set_target_properties(demo_lib PROPERTIES
    CXX_STANDARD 17
    CXX_STANDARD_REQUIRED ON
    CXX_EXTENSIONS OFF
    VERSION ${PROJECT_VERSION}
    SOVERSION ${PROJECT_VERSION_MAJOR}
)

# 设置头文件路径
target_include_directories(demo_lib
    PUBLIC 
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
    PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}
)

# 平台特定的链接选项
if(WIN32)
    target_compile_definitions(demo_lib PRIVATE PQXX_STATIC)  # 如果使用静态库
endif()

# 链接库
target_link_libraries(demo_lib
    PUBLIC
        PQXX::pqxx
        PostgreSQL::PostgreSQL
)

# 创建可执行文件
add_executable(demo_app main.cpp)

# 设置目标属性
set_target_properties(demo_app PROPERTIES
    CXX_STANDARD 17
    CXX_STANDARD_REQUIRED ON
    CXX_EXTENSIONS OFF
)

# Windows 特定设置
if(WIN32)
    # 设置子系统为控制台
    set_target_properties(demo_app PROPERTIES
        WIN32_EXECUTABLE FALSE
    )
endif()

# 链接库到可执行文件
target_link_libraries(demo_app demo_lib)

# 安装配置
install(
    TARGETS demo_lib
    EXPORT DemoProjectTargets
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

install(
    TARGETS demo_app
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

install(
    DIRECTORY include/
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
    FILES_MATCHING PATTERN "*.h"
)

install(
    EXPORT DemoProjectTargets
    FILE DemoProjectTargets.cmake
    NAMESPACE DemoProject::
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/DemoProject
)